/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 1 oct. 2010, 00:29:55
 */

package com.test.gui.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

/**
 *
 * @author user
 */
public class test extends javax.swing.JFrame implements ActionListener{

    /** Creates new form NewJFrame */
    public test() {
        initComponents();
    }

    private void initComponents() {





        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPanel1 = new JPanel();
        jTextField1 = new JTextField();


                //Create the popup menu.

       this.menuItem = new JMenuItem("A popup menu item");
        menuItem.addActionListener(this);
        jPopupMenu1.add(menuItem);
        jPopupMenu1.setPopupSize(219, 50);

                //Add listener to the text area so the popup menu can come up.
        KeyListener popupListener = new PopupListener(jPopupMenu1);
        jTextField1.addKeyListener(popupListener);



        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 125, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(126, 126, 126))
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(192, Short.MAX_VALUE))
        );

        pack();
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new test().setVisible(true);
            }
        });
    }

    private JPanel jPanel1;
    private JPopupMenu jPopupMenu1;

    private JTextField jTextField1;

    public void actionPerformed(ActionEvent e) {

    }
    // End of variables declaration
    class PopupListener extends KeyAdapter implements KeyListener{
        JPopupMenu popup;

        PopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }


        private void maybeShowPopup(KeyEvent e) {
            if(!jTextField1.getText().isEmpty() && !e.isActionKey())
            {
                popup.show(e.getComponent(),
                           0, 16);
                //popup.setEnabled(false);
                //popup.;
                 popup.setFocusable(true);
                System.out.println(popup.isFocusOwner());


       // popup.setLayout(new BorderLayout());
        //popup.add(new JPanel()); // your component
        //popup.setPopupSize(100, 100);
        popup.show(jTextField1, 0, jTextField1.getHeight());



                //jTextField1.setCaretPosition(jTextField1.getText().length());
                //jTextField1.setEnabled(true);
                jTextField1.setFocusable(true);
            }

        }

        @Override
        public void keyTyped(KeyEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            maybeShowPopup(e);
        }
    }




JMenuItem menuItem;

}

